import common from '@/utils/common'
import { types, MAX_LENGTH } from './constant.js'
import { i18n } from '@/lang'

function labelSpecificKey(str) {
  var specialKey =
    "%@[`~!#$^&*=|{}':;',\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘'"
  for (var i = 0; i < str.length; i++) {
    if (specialKey.indexOf(str.substr(i, 1)) !== -1) {
      return false
    }
  }
  return true
}

export function validateLabel(rule, value, callback) {
  if (value) {
    if (!labelSpecificKey(value)) {
      return callback(new Error(i18n.t('cmp.page_tip_specialSymbol')))
    }
    if (/^\s+$/.test(value)) {
      // 纯空格报错
      return callback(new Error(i18n.t('flyparam.invalid_input')))
    }
  } else {
    return callback()
  }
  return callback()
}

export function validateNumber(rule, value, callback) {
  if (value.length > 0) {
    if (!/^[+-1-9]?((\d+(\.\d*)?)|(\.\d+))$/.test(value)) {
      return callback(
        new Error(
          i18n.t('flyparam.invalid_input') +
            '(' +
            i18n.t('flyparam.type_number') +
            ')'
        )
      )
    }
  }
  if (value.length > 1) {
    if (/^[0]/.test(value)) {
      return callback(
        new Error(
          i18n.t('flyparam.invalid_input') +
            '(' +
            i18n.t('flyparam.type_number') +
            ')'
        )
      )
    }
  }
  if (Number(value) > rule.maxLength) {
    return callback(new Error(i18n.t('flyparam.max_number_exceeds')))
  }
  if (Number(value) < -rule.maxLength) {
    return callback(new Error(i18n.t('flyparam.number_too_small')))
  }
  callback()
}

// 引用类型的最大引用数
export function validateReferenceLength(rule, value, callback) {
  if (value) {
    if (value.length > rule.maxLength) {
      return callback(new Error(i18n.t('flyparam.max_number_exceeds')))
    }

    if (
      value.length === 0 &&
      rule.nullable === 0 &&
      rule.table.tableType !== 1
    ) {
      return callback(new Error(i18n.t('flyparam.required2')))
    }
  }
  callback()
}

export function validateHex(rule, value, callback) {
  if (value) {
    if (!/^[0-9a-fA-F]+$/.test(value)) {
      return callback(new Error(i18n.t('flyparam.invalid_input')))
    }

    if (value.length > rule.maxLength) {
      return callback(new Error(i18n.t('flyparam.length_exceeds')))
    }

    if (value.length > 0 && value.length % 2 !== 0) {
      return callback(new Error(i18n.t('flyparam.hex_even')))
    }
  }
  callback()
}

export function validateString(rule, value, callback) {
  if (value) {
    if (value.length > rule.maxLength) {
      return callback(new Error(i18n.t('flyparam.length_exceeds')))
    }
  }
  callback()
}

export function validateMaxLength(rule, value, callback) {
  if (!/^[0-9]*$/.test(value)) {
    return callback(new Error(i18n.t('flyparam.invalid_input')))
  }
  if (Number(value) > rule.maxLength) {
    this.currentItem.maxLength = rule.maxLength
  } else if (Number(value) < 1) {
    this.currentItem.maxLength = 1
  }
  callback()
}

export function validateMinLength(rule, value, callback) {
  if (!/^[0-9]*$/.test(value)) {
    return callback(new Error(i18n.t('flyparam.invalid_input')))
  }
  if (Number(value) > rule.maxLength) {
    this.currentItem.maxLength = rule.maxLength
  }
  callback()
}

export function validateInput(rule, value, callback) {
  if (value) {
    if (!common.checkSpecificKey(value)) {
      return callback(new Error(i18n.t('cmp.page_tip_specialSymbol')))
    }
    if (/^\s+$/.test(value)) {
      // 纯空格报错
      return callback(new Error(i18n.t('flyparam.invalid_input')))
    }
  } else {
    return callback()
  }
  return callback()
}

export function validateOperationSelect(rull, value, callback) {
  if (rull.operationType === 2) {
    return callback()
  }
  if (value && !value.label) {
    return callback(new Error(i18n.t('flyparam.required2')))
  }

  callback()
}

export const validateSN = [
  {
    required: true,
    message: i18n.t('device.tip_addSn')
  },
  {
    min: 6,
    max: 32,
    message: i18n.t('group.tip_nameLength', { min: 6, max: 32 })
  }
]

export const validateEntryLabel = [
  {
    required: true,
    message: i18n.t('flyparam.required2')
  }
]
